package com.tuzhi.sys.service.impl;

import com.tuzhi.base.jpa.repository.BaseRepo;
import com.tuzhi.base.service.impl.AbsBaseService;
import com.tuzhi.sys.jpa.entity.SysRoleMenuEntity;
import com.tuzhi.sys.jpa.entity.pk.SysRoleMenuPk;
import com.tuzhi.sys.jpa.repository.SysRoleMenuRepo;
import com.tuzhi.sys.service.SysRoleMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleMenuServiceImpl extends AbsBaseService<SysRoleMenuEntity, SysRoleMenuPk> implements SysRoleMenuService {

    private static final Logger log = LoggerFactory.getLogger(SysRoleMenuServiceImpl.class);

    @Autowired
    SysRoleMenuRepo repo;

    @Override
    protected BaseRepo getThisRepo() {
        return repo;
    }

    @Override
    public List<Long> listMenuId4Role(Long roleId) {
        List<Long> listId = new ArrayList<>();
        SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
        sysRoleMenuEntity.setRoleId(roleId);
        List<SysRoleMenuEntity> sysRoleMenuEntityList = findAll(sysRoleMenuEntity);
        for (SysRoleMenuEntity entity : sysRoleMenuEntityList) {
            listId.add(entity.getMenuId());
        }
        return listId;
    }

    @Override
    public int countByRoleId(Long roleId) {
        return repo.countByRoleId(roleId);
    }

    @Override
    @Transactional
    public int deleteByRoleId(Long roleId) {
        int count = countByRoleId(roleId);
        if (count > 0) {
            return repo.deleteByRoleId(roleId);
        }
        return 0;
    }

    @Override
    public int countByMenuId(Long menuId) {
        return repo.countByMenuId(menuId);
    }

    @Override
    public int deleteByMenuId(Long menuId) {
        int count = countByMenuId(menuId);
        if (count > 0) {
            return repo.deleteByMenuId(menuId);
        }
        return 0;
    }
}
